/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.domain.SysDispatchUnit;
import me.zhengjie.modules.system.service.SysDispatchUnitService;
import me.zhengjie.modules.system.domain.vo.SysDispatchUnitQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author qiang
* @date 2023-09-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "DisUnit管理")
@RequestMapping("/api/sysDispatchUnit")
public class SysDispatchUnitController {

    private final SysDispatchUnitService sysDispatchUnitService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysDispatchUnit:list')")
    public void exportSysDispatchUnit(HttpServletResponse response, SysDispatchUnitQueryCriteria criteria) throws IOException {
        sysDispatchUnitService.download(sysDispatchUnitService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询DisUnit")
    @ApiOperation("查询DisUnit")
    @PreAuthorize("@el.check('sysDispatchUnit:list')")
    public ResponseEntity<PageResult<SysDispatchUnit>> querySysDispatchUnit(String name, String type, Page<Object> page){
        return new ResponseEntity<>(sysDispatchUnitService.queryAll(name , type ,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增DisUnit")
    @ApiOperation("新增DisUnit")
    @PreAuthorize("@el.check('sysDispatchUnit:add')")
    public ResponseEntity<Object> createSysDispatchUnit(@Validated @RequestBody SysDispatchUnit resources){
        sysDispatchUnitService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改DisUnit")
    @ApiOperation("修改DisUnit")
    @PreAuthorize("@el.check('sysDispatchUnit:edit')")
    public ResponseEntity<Object> updateSysDispatchUnit(@Validated @RequestBody SysDispatchUnit resources){
        sysDispatchUnitService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除DisUnit")
    @ApiOperation("删除DisUnit")
    @PreAuthorize("@el.check('sysDispatchUnit:del')")
    public ResponseEntity<Object> deleteSysDispatchUnit(@RequestBody List<Long> ids) {
        sysDispatchUnitService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/getShortName")
    @PreAuthorize("@el.check('sysDispatchUnit:list')")
    public ResponseEntity<List<DictDetail>> getShortName(){
        ArrayList<DictDetail> list = sysDispatchUnitService.getShortName();
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @GetMapping(value = "/getTypes")
    @PreAuthorize("@el.check('sysDispatchUnit:list')")
    public ResponseEntity<List<String>> getTypes(){
        ArrayList<String> list = sysDispatchUnitService.getTypeNames();
        return new ResponseEntity<>(list,HttpStatus.OK);
    }
}